﻿namespace SmartSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;using NLog;

    public class DmPBSPDataAccess
    {
        private static Logger log = LogManager.GetCurrentClassLogger();
        public void CreateInvoice(string ps_Subsidiary, object pd_Ngay_Ct, object ps_Dien_Giai, object ps_Desc_EN, object ps_Ma_Tte, object ps_UserName, object pi_Posted, object pb_Locked, DataTable poDetail)
        {
            if ((poDetail != null) && (poDetail.Rows.Count != 0))
            {
                string str = SSDataAccess.Cong_So_Tt(ps_Subsidiary);
                int num = 0;
                CTKTDataAccess access = new CTKTDataAccess();
                object obj2 = access.Cong_So_Ct(ps_Subsidiary, "PKSP", DBNull.Value, pd_Ngay_Ct, DBNull.Value);
                decimal num2 = 0M;
                SqlConnection SSConnection = SSDataAccess.SSConnection;
                SqlTransaction transaction = SSConnection.BeginTransaction();
                SqlCommand command = new SqlCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Transaction = transaction;
                SqlParameter[] values = new SqlParameter[0x13];
                foreach (DataRow row in poDetail.Rows)
                {
                    num2 += (decimal) row["Tien"];
                    num++;
                    values[0] = new SqlParameter("p_Stt0", row["Stt0_No"]);
                    values[1] = new SqlParameter("p_Stt", str);
                    values[2] = new SqlParameter("p_Stt_NV", num);
                    values[3] = new SqlParameter("p_No_Co", "N");
                    values[4] = new SqlParameter("p_Tk", row["Tk_No"]);
                    values[5] = new SqlParameter("p_Ma_Tte", ps_Ma_Tte);
                    values[6] = new SqlParameter("p_Ty_Gia", 1);
                    values[7] = new SqlParameter("p_Ty_Gia_Ht", 1);
                    values[8] = new SqlParameter("p_Tien_Nt", row["Tien"]);
                    values[9] = new SqlParameter("p_Tien", row["Tien"]);
                    values[10] = new SqlParameter("p_Ma_Bp", DBNull.Value);
                    values[11] = new SqlParameter("p_Ma_Hd", DBNull.Value);
                    values[12] = new SqlParameter("p_Ma_Dt", DBNull.Value);
                    values[13] = new SqlParameter("p_Han_Tt", DBNull.Value);
                    values[14] = new SqlParameter("p_Ma_Km", DBNull.Value);
                    values[15] = new SqlParameter("p_Ma_Sp", DBNull.Value);
                    values[0x10] = new SqlParameter("p_Dien_Giai", ps_Dien_Giai);
                    values[0x11] = new SqlParameter("p_Desc_EN", ps_Desc_EN);
                    values[0x12] = new SqlParameter("p_Result", SqlDbType.BigInt);
                    values[0x12].Direction = ParameterDirection.Output;
                    values[0x12].SqlDbType = SqlDbType.Int;
                    try
                    {
                        command.CommandText = "CtK0_Save";
                        command.Parameters.Clear();
                        command.Parameters.AddRange(values);
                        command.ExecuteNonQuery();
                    }
                    catch (Exception exception)
                    {
                        log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
                    }
                    num++;
                    values[0] = new SqlParameter("p_Stt0", row["Stt0_Co"]);
                    values[1] = new SqlParameter("p_Stt", str);
                    values[2] = new SqlParameter("p_Stt_NV", num);
                    values[3] = new SqlParameter("p_No_Co", "C");
                    values[4] = new SqlParameter("p_Tk", row["Tk_Co"]);
                    values[5] = new SqlParameter("p_Ma_Tte", ps_Ma_Tte);
                    values[6] = new SqlParameter("p_Ty_Gia", 1);
                    values[7] = new SqlParameter("p_Ty_Gia_Ht", 1);
                    values[8] = new SqlParameter("p_Tien_Nt", row["Tien"]);
                    values[9] = new SqlParameter("p_Tien", row["Tien"]);
                    values[10] = new SqlParameter("p_Ma_Bp", DBNull.Value);
                    values[11] = new SqlParameter("p_Ma_Hd", DBNull.Value);
                    values[12] = new SqlParameter("p_Ma_Dt", DBNull.Value);
                    values[13] = new SqlParameter("p_Han_Tt", DBNull.Value);
                    values[14] = new SqlParameter("p_Ma_Km", DBNull.Value);
                    values[15] = new SqlParameter("p_Ma_Sp", DBNull.Value);
                    values[0x10] = new SqlParameter("p_Dien_Giai", ps_Dien_Giai);
                    values[0x11] = new SqlParameter("p_Desc_EN", ps_Desc_EN);
                    values[0x12] = new SqlParameter("p_Result", SqlDbType.BigInt);
                    values[0x12].Direction = ParameterDirection.Output;
                    values[0x12].SqlDbType = SqlDbType.Int;
                    try
                    {
                        command.CommandText = "CtK0_Save";
                        command.Parameters.Clear();
                        command.Parameters.AddRange(values);
                        command.ExecuteNonQuery();
                        continue;
                    }
                    catch (Exception exception2)
                    {
                        log.Error(exception2.Message); throw new NotSupportedException("Invalid expression", exception2);
                    }
                }
                SqlParameter[] parameterArray2 = new SqlParameter[] { 
                    new SqlParameter("p_Moi_Sua", true), new SqlParameter("p_Stt", str), new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Nh_Ct", "3"), new SqlParameter("p_Ma_Ct", "PKSP"), new SqlParameter("p_Ngay_Ps", pd_Ngay_Ct), new SqlParameter("p_Ngay_Ct", pd_Ngay_Ct), new SqlParameter("p_BookNum", DBNull.Value), new SqlParameter("p_So_Ct", obj2), new SqlParameter("p_Ma_Bp", DBNull.Value), new SqlParameter("p_Ma_Hd", DBNull.Value), new SqlParameter("p_Ma_Dt", DBNull.Value), new SqlParameter("p_Ong_Ba", DBNull.Value), new SqlParameter("p_Dia_Chi", DBNull.Value), new SqlParameter("p_Dien_Giai", ps_Dien_Giai), new SqlParameter("p_Desc_EN", ps_Desc_EN), 
                    new SqlParameter("p_Ma_Tte", ps_Ma_Tte), new SqlParameter("p_Ty_Gia", 1), new SqlParameter("p_Ty_Gia_Ht", 1), new SqlParameter("p_TTien_NtN", num2), new SqlParameter("p_TTien_NtC", num2), new SqlParameter("p_TTien", num2), new SqlParameter("p_TTien_VAT0", DBNull.Value), new SqlParameter("p_IL_TTien", DBNull.Value), new SqlParameter("p_UN_TTien", DBNull.Value), new SqlParameter("p_Ct_Di_Kem", DBNull.Value), new SqlParameter("p_Ma_Loai1", DBNull.Value), new SqlParameter("p_Ma_Loai2", DBNull.Value), new SqlParameter("p_Ma_Loai3", DBNull.Value), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Posted", pi_Posted), new SqlParameter("p_Locked", pb_Locked), 
                    new SqlParameter("p_Result", SqlDbType.BigInt)
                 };
                parameterArray2[0x20].Direction = ParameterDirection.Output;
                parameterArray2[0x20].SqlDbType = SqlDbType.Int;
                try
                {
                    command.CommandText = "CtK_Save";
                    command.Parameters.Clear();
                    command.Parameters.AddRange(parameterArray2);
                    command.ExecuteNonQuery();
                }
                catch (Exception exception3)
                {
                    log.Error(exception3.Message); throw new NotSupportedException("Invalid expression", exception3);
                }
                access.PostSoCai(SSConnection, transaction, str, pi_Posted);
                transaction.Commit();
                SSDataAccess.CloseConnection(SSConnection);
            }
        }

        public bool DeleteDetailRowC(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", po_KeyCode), new SqlParameter() };
            values[1].Direction = ParameterDirection.ReturnValue;
            values[1].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "ZHeSoC_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public bool DeleteDetailRowHS(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", po_KeyCode), new SqlParameter() };
            values[1].Direction = ParameterDirection.ReturnValue;
            values[1].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "ZHeSo_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public DataSet GetDataSet(string ps_Subsidiary, object ps_Loai_PB)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Loai_PB", ps_Loai_PB) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "ZPhanBo_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public DataSet GetDataSetC(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "ZPhanBoC_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public DataTable GetDetailTable(object ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object ps_Ma_Sp)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_Ma_Sp", ps_Ma_Sp), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[4].Direction = ParameterDirection.Output;
            values[4].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DetailProductCosting";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public DataTable GetDetailTableC(object ps_KeyCode)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_KeyCode) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "ZHeSoC_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public DataTable GetDetailTableHS(object ps_KeyCode)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_KeyCode) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "ZHeSo_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public DataTable GetMasterTable(object ps_Subsidiaries, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object ps_Ma_Sp_Me)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs_List", ps_Subsidiaries), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_Ma_Sp_Me", ps_Ma_Sp_Me), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[4].Direction = ParameterDirection.Output;
            values[4].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "MasterProductCosting";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public DataTable PhanBo_CHSP(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object ps_Ma_Sp_Me, object ps_Ma_Yt, ref object pi_TTien)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_Ma_Sp_Me", ps_Ma_Sp_Me), new SqlParameter("p_Ma_Yt", ps_Ma_Yt), new SqlParameter("p_TTien", pi_TTien) };
            values[5].Direction = ParameterDirection.Output;
            values[5].SqlDbType = SqlDbType.Decimal;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataTable dataTable = new DataTable("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "PhanBo_CHSP";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                pi_TTien = values[5].Value;
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return table2;
        }

        public void PhanBo_Delete_All(object ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object ps_Ma_Sp_Me)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_Ma_Sp_Me", ps_Ma_Sp_Me) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "PhanBo_Delete_All";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
        }

        public int PhanBo_DMSP(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object pi_ZPBID, object ps_Tk_154, object ps_UserName)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_ZPBID", pi_ZPBID), new SqlParameter("p_Tk_154", ps_Tk_154), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[6].Direction = ParameterDirection.Output;
            values[6].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "PhanBo_DMSP";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[6].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }

        public int PhanBo_HSSP(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object pi_ZPBID, object ps_Tk_154, object ps_UserName)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_ZPBID", pi_ZPBID), new SqlParameter("p_Tk_154", ps_Tk_154), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[6].Direction = ParameterDirection.Output;
            values[6].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "PhanBo_HSSP";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[6].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }

        public int PhanBo_TLSP(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object pi_ZPBID, object ps_Tk_154, object ps_UserName)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_ZPBID", pi_ZPBID), new SqlParameter("p_Tk_154", ps_Tk_154), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[6].Direction = ParameterDirection.Output;
            values[6].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "PhanBo_TLSP";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[6].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }

        public int PhanBo_TTSP(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object pi_ZPBID, object ps_Tk_154, object ps_UserName)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_ZPBID", pi_ZPBID), new SqlParameter("p_Tk_154", ps_Tk_154), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[6].Direction = ParameterDirection.Output;
            values[6].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "PhanBo_TTSP";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[6].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }

        public void ReAllocation(object ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object ps_Ma_Sp, object ps_Ma_Yt, object pi_Tien)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_Ma_Sp", ps_Ma_Sp), new SqlParameter("p_Ma_Yt", ps_Ma_Yt), new SqlParameter("p_Tien", pi_Tien) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            new DataTable();
            new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "ReAllocation";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
        }

        private int SaveDetailRowC(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_MasterKey, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", row["Stt0"]), new SqlParameter("p_Stt", po_MasterKey), new SqlParameter("p_Ma_Sp", row["Ma_Sp"]), new SqlParameter("p_He_So", row["He_So"]), new SqlParameter("p_Tien_PB", row["Tien_PB"]), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[5].Direction = ParameterDirection.Output;
            values[5].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "ZHeSoC_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[5].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        private int SaveDetailRowHS(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_MasterKey, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", row["Stt0"]), new SqlParameter("p_Stt", po_MasterKey), new SqlParameter("p_Ma_Sp", row["Ma_Sp"]), new SqlParameter("p_He_So", row["He_So"]), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[4].Direction = ParameterDirection.Output;
            values[4].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "ZHeSo_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[4].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public int SaveMasterRowC(SqlConnection po_Connection, SqlTransaction po_Transaction, bool pb_NewChecked, object pd_Ngay_Ct1, object pd_Ngay_Ct2, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_Dvcs", row["Ma_Dvcs"]), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_Ma_Yt", row["Ma_Yt"]), new SqlParameter("p_Ma_Sp", row["Ma_Sp"]), new SqlParameter("p_Dien_Giai", row["Dien_Giai"]), new SqlParameter("p_Desc_EN", row["Desc_EN"]), new SqlParameter("p_TTien_PB", row["TTien_PB"]), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[1].Direction = ParameterDirection.InputOutput;
            values[1].SqlDbType = SqlDbType.BigInt;
            values[10].Direction = ParameterDirection.Output;
            values[10].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "ZPhanBoC_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                row["Stt"] = values[1].Value;
                num = (int) values[10].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public int SaveMasterRowHS(SqlConnection po_Connection, SqlTransaction po_Transaction, bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_Dvcs", row["Ma_Dvcs"]), new SqlParameter("p_Loai_PB", row["Loai_PB"]), new SqlParameter("p_Ma_Yt", row["Ma_Yt"]), new SqlParameter("p_Ma_Sp", row["Ma_Sp"]), new SqlParameter("p_Dien_Giai", row["Dien_Giai"]), new SqlParameter("p_Desc_EN", row["Desc_EN"]), new SqlParameter("p_Ma_Yt0", row["Ma_Yt0"]), new SqlParameter("p_Yt_Di01", row["Yt_Di01"]), new SqlParameter("p_Yt_Di02", row["Yt_Di02"]), new SqlParameter("p_Yt_Di03", row["Yt_Di03"]), new SqlParameter("p_Yt_Di04", row["Yt_Di04"]), new SqlParameter("p_Yt_Di05", row["Yt_Di05"]), new SqlParameter("p_Loai_Yt", row["Loai_Yt"]), new SqlParameter("p_SLuong_Ck", row["SLuong_Ck"]), 
                new SqlParameter("p_Lay_DL_TH", row["Lay_DL_TH"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[1].Direction = ParameterDirection.InputOutput;
            values[1].SqlDbType = SqlDbType.BigInt;
            values[0x11].Direction = ParameterDirection.Output;
            values[0x11].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "ZPhanBo_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                row["Stt"] = values[1].Value;
                num = (int) values[0x11].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public int SaveRow(bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_Dvcs", row["Ma_Dvcs"]), new SqlParameter("p_Loai_PB", row["Loai_PB"]), new SqlParameter("p_Ma_Yt", row["Ma_Yt"]), new SqlParameter("p_Ma_Sp", row["Ma_Sp"]), new SqlParameter("p_Dien_Giai", row["Dien_Giai"]), new SqlParameter("p_Desc_EN", row["Desc_EN"]), new SqlParameter("p_Ma_Yt0", row["Ma_Yt0"]), new SqlParameter("p_Yt_Di01", row["Yt_Di01"]), new SqlParameter("p_Yt_Di02", row["Yt_Di02"]), new SqlParameter("p_Yt_Di03", row["Yt_Di03"]), new SqlParameter("p_Yt_Di04", row["Yt_Di04"]), new SqlParameter("p_Yt_Di05", row["Yt_Di05"]), new SqlParameter("p_Loai_Yt", row["Loai_Yt"]), new SqlParameter("p_SLuong_Ck", row["SLuong_Ck"]), 
                new SqlParameter("p_Lay_DL_TH", row["Lay_DL_TH"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[1].Direction = ParameterDirection.InputOutput;
            values[1].SqlDbType = SqlDbType.BigInt;
            values[0x11].Direction = ParameterDirection.Output;
            values[0x11].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "ZPhanBo_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                row["Stt"] = values[1].Value;
                num = (int) values[0x11].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }

        public bool SaveRowC(bool pb_NewChecked, object pd_Ngay_Ct1, object pd_Ngay_Ct2, DataRow po_MasterDataRow, DataTable po_DetailDataTable)
        {
            bool flag = false;
            foreach (DataRow row in po_DetailDataTable.Rows)
            {
                if ((row.RowState != DataRowState.Deleted) && ((((decimal) row["Tien_PB"]) != 0M) || (((decimal) row["He_So"]) != 0M)))
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                SSDataAccess.Delete("ZPhanBoC_Delete", po_MasterDataRow["Stt"]);
                return false;
            }
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlTransaction transaction = SSConnection.BeginTransaction();
            flag = true;
            if (this.SaveMasterRowC(SSConnection, transaction, pb_NewChecked, pd_Ngay_Ct1, pd_Ngay_Ct2, po_MasterDataRow) == 1)
            {
                foreach (DataRow row2 in po_DetailDataTable.Rows)
                {
                    row2.EndEdit();
                    if ((row2.RowState != DataRowState.Deleted) && ((((decimal) row2["Tien_PB"]) != 0M) || (((decimal) row2["He_So"]) != 0M)))
                    {
                        if (((row2.RowState != DataRowState.Added) && (row2.RowState != DataRowState.Modified)) || (this.SaveDetailRowC(SSConnection, transaction, po_MasterDataRow["Stt"], row2) == 1))
                        {
                            continue;
                        }
                        flag = false;
                        break;
                    }
                    if (row2.RowState == DataRowState.Deleted)
                    {
                        this.DeleteDetailRowC(SSConnection, transaction, row2["Stt0", DataRowVersion.Original]);
                    }
                    else
                    {
                        this.DeleteDetailRowC(SSConnection, transaction, row2["Stt0"]);
                    }
                }
                if (flag)
                {
                    po_DetailDataTable.AcceptChanges();
                    po_MasterDataRow.Table.AcceptChanges();
                    transaction.Commit();
                    SSDataAccess.CloseConnection(SSConnection);
                    return true;
                }
            }
            transaction.Rollback();
            SSDataAccess.CloseConnection(SSConnection);
            return false;
        }

        public bool SaveRowHS(bool pb_NewChecked, DataRow po_MasterDataRow, DataTable po_DetailDataTable)
        {
            bool flag = false;
            foreach (DataRow row in po_DetailDataTable.Rows)
            {
                if ((row.RowState != DataRowState.Deleted) && (((decimal) row["He_So"]) != 0M))
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                SSDataAccess.Delete("ZPhanBo_Delete", po_MasterDataRow["Stt"]);
                return false;
            }
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlTransaction transaction = SSConnection.BeginTransaction();
            flag = true;
            if (this.SaveMasterRowHS(SSConnection, transaction, pb_NewChecked, po_MasterDataRow) == 1)
            {
                foreach (DataRow row2 in po_DetailDataTable.Rows)
                {
                    row2.EndEdit();
                    if ((row2.RowState != DataRowState.Deleted) && (((decimal) row2["He_So"]) != 0M))
                    {
                        if (((row2.RowState != DataRowState.Added) && (row2.RowState != DataRowState.Modified)) || (this.SaveDetailRowHS(SSConnection, transaction, po_MasterDataRow["Stt"], row2) == 1))
                        {
                            continue;
                        }
                        flag = false;
                        break;
                    }
                    if (row2.RowState == DataRowState.Deleted)
                    {
                        this.DeleteDetailRowHS(SSConnection, transaction, row2["Stt0", DataRowVersion.Original]);
                    }
                    else
                    {
                        this.DeleteDetailRowHS(SSConnection, transaction, row2["Stt0"]);
                    }
                }
                if (flag)
                {
                    po_DetailDataTable.AcceptChanges();
                    po_MasterDataRow.Table.AcceptChanges();
                    transaction.Commit();
                    SSDataAccess.CloseConnection(SSConnection);
                    return true;
                }
            }
            transaction.Rollback();
            SSDataAccess.CloseConnection(SSConnection);
            return false;
        }

        public int UpdateProductCosting(object ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object ps_Ma_Sp_Me, object ps_Tk_154)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_Ma_Sp_Me", ps_Ma_Sp_Me), new SqlParameter("p_Tk_154", ps_Tk_154), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[5].Direction = ParameterDirection.Output;
            values[5].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UpdateProductCosting";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[5].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }
    }
}

